package com.briup.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.system.pojo.SysUser;
import com.briup.system.service.ISysRoleService;
import com.briup.system.service.SysUserService;
import com.briup.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private ISysRoleService roleService;


    public boolean saveOrUpdate(SysUser entity) {
        // 验证entity是更新还是新增
        if(entity.getUsrId() == null){
            // 新增
            // 因为roleId是必填项，所以可以根据roleId查询出roleName作为默认值
            String tmp = roleService.getNameById(entity.getUsrRoleId());
            String roleName = entity.getUsrRoleName()==null?tmp:entity.getUsrRoleName();
            Integer flag = entity.getUsrFlag() == null?1:entity.getUsrFlag();
            String password = entity.getUsrPassword() == null?"123456":entity.getUsrPassword();
            // 重新给默认值赋值
            entity.setUsrRoleName(roleName);
            entity.setUsrFlag(flag);
            entity.setUsrPassword(password);
        }

        return super.saveOrUpdate(entity);
    }
}




